<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="../assets/css/bootstrap.min.css" />
    <script src="../assets/js/jquery.min.js"></script>
    <script src="../assets/js/bootstrap.min.js"></script>
    <script src="../assets/js/DatePicker.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
    <link rel="stylesheet" href="../assets/css/style.css">
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <title>商品修改</title>
    <script>
        $(function () {
            $("#datepicker").datepicker({
                //inline:true,
                changeMonth: true, //在日期的标题栏中是否出现下拉选择框，选择日期中的月份。true代表有选择框，false代表无选择框
                changeYear: false, //在日期的标题栏中是否出现下拉选择框，选择日期中的年份。true代表有选择框，false代表无选择框
                showButtonPanel: true,  //在日期面板的下方出现两个按钮，一个是今天，一个是关闭。默认值是false,不显示的
                closeText: "关闭",  //必须结合showButtonPanel使用，并且showButtonPanel的值必须是true，否则看不到效果
                dateFormat: "yy-mm-dd",  //表示日期显示的格式
                defaultDate: -3,  //缺省值在当前日期中加或减几天
                showAnim: "toggle",  //日期界面出现的动画效果
                showWeek: true,    //显示周  当前中的第几周
                firstDay: 1,   //默认firstDay 当前的第一天
                yearRange: "1970:2021",   //表示下拉框中年份的范围
                onSelect: function (selectedDate) {
                    //alert(selectedDate);
                    //获取当前对象this.id=dp minDate  //开始日期的最小值
                    //获取 option的值是minDat或者是maxDate  根据dom对象的
                    var option = this.id == "dp" ? "minDate" : "maxDate",
                        //获取当前日期的实例对象，每遍历一次就获取一次
                        instance = $(this).data("datepicker"),
                        //获取日期   instance.settings 获取日期的值
                        date = $.datepicker.parseDate(
                            instance.settings.dateFormat ||
                            $.datepicker._defaults.dateFormat,
                            selectedDate, instance.settings);
                    //设置日期
                    dates.not(this).datepicker("option", option, date);
                }
            });
        });
    </script>
</head>
<body>
<div class="row" style="margin-left: 20px;">
    <form id="modifyfile" action="" method="post" enctype="multipart/form-data">
        <div>
            <h3>修改商品</h3>
        </div>
        <hr />
        <div class="row">
            <div class="col-sm-6">
                <p style="color:red"></p>
                <div class="form-group form-inline">
                    <label>名称:</label>
                    <input type="text" name="name" class="form-control" />
                </div>
                <div class="form-group form-inline">
                    <label>分类:</label>
                    <select name="typeName" class="form-control" id="tb_list">
                        <option value="0" >---请选择商品类型---</option>
                        <!--							这里加接口获得的类型-->

                    </select>
                </div>
                <div class="form-group form-inline">
                    <label>时间:</label>
                    <input type="text" id="datepicker" name="pubdate" class="form-control"/>
<!--                    <input type="text" name="pubdate" readonly="readonly" class="form-control" onclick="setday(this)" />-->
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group form-inline">
                    <label>价格:</label>
                    <input type="text" name="price" class="form-control" /> 元
                </div>
                <div class="form-group form-inline">
                    <label>评分:</label>
                    <input type="text" name="star" class="form-control" />
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-10">
                <div class="form-group form-inline">
                    <label>商品图片</label>
                    <input type="file" name="file" id="photoFile"/>
                </div>
                <div class="form-group ">
                    <label>商品简介</label>
                    <textarea  name="intro" class="form-control" rows="5"></textarea>
                </div>
                <div class="form-group form-inline">
                    <input type="submit" value="修改" onclick="modify()" class="btn btn-primary" />
                    <input type="reset" value="重置" class="btn btn-default" />
                </div>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id= location.search.replace(/[^\d]/g, "");
    console.log('id'+id);
    /*添加select*/
    $(function(){
        $.ajax({
            url:"http://localhost:80/phalapi/public/index.php?s=App.GoodsType.GetGoodsTypes",
            type: "GET",
            dataType: "JSON",
            success: function (result) {
                var data = result.data.info;
                console.log('id'+id);

                for(i=0; i<data.length; i++){
                    console.log('data'+data[i]['name']);
                    var obj = document.getElementById("tb_list");
                    obj.options.add(new Option(data[i]['name'],data[i]['name']));
                }

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })

    var filePic
    //查询全部商品类别
    $(function () {
        $.ajax({
            url:"http://localhost:80/phalapi/public/index.php?s=App.Goods.GetGoodInfo",
            type: "GET",
            data:{
                "good_id" : id
            },
            dataType: "JSON",
            success: function (result) {
                var data = result.data.info;

                console.log('data'+data['name']);


                var name = data['name'];
                var typeName = data['typeName'];
                var pubdate = data['pubdate'];
                var price = data['price'];
                var star = data['star'];
                filePic = data['picture'];
                var intro = data['intro'];

                $("input[name='name']").val(name);

                var obj = document.getElementById("tb_list");
                for (var i = 0; i < obj.options.length; i++){
                    if (obj.options[i].value == typeName){
                        obj.options[i].selected = true;
                        break;
                    }
                }
                // if($("select[name='typeName']").text == typeName)

                $("input[name='pubdate']").val(pubdate);
                $("input[name='price']").val(price);
                $("input[name='star']").val(star);
                // $("input[name='file']").val(file);
                $("textarea[name='intro']").val(intro);

                // $("#tb_list").html(s);
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })

    function getFileName(o) {
        var pos = o.lastIndexOf("\\");
        return o.substring(pos + 1);
    }

    function modify(){

        var good_id = id;
        var name = $("input[name='name']").val();
        var typeName = $("#tb_list option:selected").val();
        var pubdate = $("input[name='pubdate']").val();
        var price = $("input[name='price']").val();
        var star = $("input[name='star']").val();
        var file = $("input[name='file']").val()
        var intro = $("textarea[name='intro']").val();
        console.log('input:::'+name,typeName,pubdate,price,star,file,intro)

        var url = "202106/";
        var fileName = getFileName(file);

        $("#modifyfile").attr("action","fileupload.php?filename="+fileName)


        if (file==''){
            file = filePic
        }

        $.ajax({
            url:"http://localhost:80/phalapi/public/index.php?s=App.Goods.UpdateGoodsInfo",
            type: "POST",
            contentType:"application/json;charset=utf-8",
            data:JSON.stringify({
                "good_id" : id,
                "good_name" : name,
                "good_price" : price,
                "good_typeName" : typeName,
                "good_pubdate" : pubdate,
                "good_picture" : url + fileName,
                "good_intro" : intro
            }),
            dataType: "JSON",
            success: function (result) {
                var data = result.data.info;
                console.log('id'+id);
                console.log('成功');
                alert('修改成功');
                // for(i=0; i<data.length; i++){
                //     console.log('data'+data[i]['name']);
                // }
                window.location.replace("showGoods.php");
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    }

</script>
</body>
</html>
